/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import java.util.Map;

import cacao.client.CaCaO;
import cacao.client.data.IndexEntryObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;



public class EditIndexCmp extends Composite implements CaCaOComposite{
	private CaCaO application;
	private PushButton btnCreate, btnDelete, btnStore;
	private Grid grid;
	private TextBox tbEntry;


	public EditIndexCmp(CaCaO app){ 
		application=app;
		// Grids must be sized explicitly, though they can be resized later.
		grid = new Grid(6,6);
		// Put some values in the grid cells.
		grid.setWidget(0,0, new HTML(application.getMessages().editIndexHeadline()));
		grid.setWidget(0,1, new Label(application.getMessages().editIndexCardID()));
		grid.setWidget(0,2, new Label(application.getMessages().editIndexOffset()));
		grid.setWidget(0,3, new Label(application.getMessages().editIndexEntry()));
		grid.setWidget(0,4, new Label(application.getMessages().editIndexNormEntry()));
		grid.setWidget(0,5, new Label(application.getMessages().editIndexOrder()));
	
		grid.setWidget(1,0, new Label(application.getMessages().editIndexPredecessor2nd()));
		grid.setWidget(2,0, new Label(application.getMessages().editIndexPredecessor()));
		grid.setWidget(3,0, new Label(application.getMessages().editIndexEdit()));
		grid.setWidget(4,0, new Label(application.getMessages().editIndexSuccessor()));
		grid.setWidget(5,0, new Label(application.getMessages().editIndexSuccessor2nd()));
		
		tbEntry = new TextBox();
		tbEntry.setWidth("250px");
		
		btnStore = new PushButton(application.getMessages().editIndexSave());
		btnStore.setWidth("100px");
		btnStore.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				IndexEntryObject data = new IndexEntryObject();
				String cardID = application.getCurrentItem().getId();
				String catalogCode = cardID.split("_")[0];
				data.setCatalogCode(catalogCode);
				data.setCardID(cardID);
				data.setText(tbEntry.getText());
				
				AsyncCallback<String> callback2 = new AsyncCallback<String>(){
					public void onSuccess(String result2){
						if(result2!=null){
							Window.alert(application.getMessages().editCardFailureNotSaved(result2));
						}
					}
					public void onFailure(Throwable caught){
						Window.alert(application.getMessages().editCardFailureDisplay());
					}
				};
				application.getDataService().insertOrUpdateIndexEntry(data, callback2);
			}
		});
		
		btnCreate = new PushButton(application.getMessages().editIndexCreate());
		btnCreate.setWidth("100px");
		btnCreate.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				IndexEntryObject data = new IndexEntryObject();
				String cardID = application.getCurrentItem().getId();
				String catalogCode = cardID.split("_")[0];
				data.setCatalogCode(catalogCode);
				data.setCardID(cardID);
				data.setText(null);
				data.setNormText(null);

				AsyncCallback<String> callback2 = new AsyncCallback<String>(){
					public void onSuccess(String result2){
						if(result2!=null){
							Window.alert(application.getMessages().editCardFailureNotSaved(result2));
						}
					}
					public void onFailure(Throwable caught){
						Window.alert(application.getMessages().editCardFailureDisplay());
					}
				};
				application.getDataService().insertOrUpdateIndexEntry(data, callback2);
			}
		});
		
		btnDelete = new PushButton(application.getMessages().editIndexSave());
		btnDelete.setWidth("100px");
		btnDelete.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				AsyncCallback<String> callback2 = new AsyncCallback<String>(){
					public void onSuccess(String result2){
						if(result2!=null){
							Window.alert(application.getMessages().editCardFailureNotSaved(result2));
						}
					}
					public void onFailure(Throwable caught){
						Window.alert(application.getMessages().editCardFailureDisplay());
					}
				};
				application.getDataService().deleteIndexEntry(application.getCurrentItem().getId(), callback2);
			}
		});
		
		initWidget(grid);
	}

	public void updateUI(){
		AsyncCallback<Map<Integer, IndexEntryObject>> callback = new AsyncCallback<Map<Integer, IndexEntryObject>>(){
			public void onSuccess(Map<Integer, IndexEntryObject> result){
				

			}
			public void onFailure(Throwable caught){
				Window.alert(application.getMessages().editCardFailureDisplay());
			}
		};
		
		application.getDataService().retrieveIndexSection(application.getCurrentItem().getId(), callback);
	}

}
